package com.codingfly.service.impl;

import com.codingfly.config.GlobalConfig;
import com.codingfly.core.exception.AuthenticationException;
import com.codingfly.core.util.TokenUtils;
import com.codingfly.service.OauthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class OauthServiceImpl implements OauthService {
    private static Logger logger = LoggerFactory.getLogger(OauthServiceImpl.class);

    /**
     * 校验token是否有效
     * @return
     */
    @Override
    public Boolean checkAccessTokenValid() {
        String accessToken = TokenUtils.getAccessToken();
        if (ObjectUtils.isEmpty(accessToken)) {
            return false;
        }
        return ObjectUtils.isEmpty(GlobalConfig.tokenStorage.getIfPresent(accessToken))? false:true;
    }

    /**
     * 校验token是否有效
     * @return
     */
    @Override
    public void checkAccessTokenValidErr() {
        if (checkAccessTokenValid()==false) {
            throw new AuthenticationException();
        }
    }

}
